import { t } from 'i18next'
import {
	fetchPostRecordPauseOrPlay,
	fetchPostRecordPlay,
	fetchPostRecordStop,
	fetchStopMix,
} from '@/api/manager/playVideo'
import { FetchPlayVideoInfo, FetchPostRecordPlayParamsProps } from '@/api/manager/playVideo/interface'
import Alert from '@/components/Alert'
import { useMccStore } from '../../store'
import { fetchStopVoiceInspire } from '@/api/manager/voiceInspire'
/**停止会议混音 */
export const stopMix = (mixMode: number, callback: () => void) => {
	let meetingMoid = useMccStore.getState().mccInfo.meetingMoid
	fetchStopMix(meetingMoid, mixMode)
		.then(() => {
			callback()
		})
		.catch(() => {
			Alert(t('结束混音失败'))
		})
}
/**结束语音激励 */
export const voiceInspire = (callback: () => void) => {
	let meetingMoid = useMccStore.getState().mccInfo.meetingMoid
	fetchStopVoiceInspire(meetingMoid)
		.then(() => {
			callback()
		})
		.catch(() => {
			Alert(t('结束语音激励失败'))
		})
}
/**关闭混音和语音激励 */
export const voiceInspireAndStopMix = (mixMode: number, callback: () => void) => {
	let meetingMoid = useMccStore.getState().mccInfo.meetingMoid
	fetchStopVoiceInspire(meetingMoid)
		.then(() => {
			stopMix(mixMode, callback)
		})
		.catch(() => {
			Alert(t('结束语音激励失败'))
		})
}
export const startMeetingPlayback = (playVideoBackInfo: FetchPlayVideoInfo) => {
	let meetingMoid = useMccStore.getState().mccInfo.meetingMoid
	let name = playVideoBackInfo?.name || ''
	if (name.indexOf('.asf') === -1) {
		name = name + '.asf'
	}
	let params: FetchPostRecordPlayParamsProps = {
		name: name,
		duration: playVideoBackInfo?.duration || '0',
		playback_duration: playVideoBackInfo?.playback_duration || '0',
		file_id: playVideoBackInfo?.file_id || '',
		vrs_id: playVideoBackInfo?.vrs_id || '',
	}
	fetchPostRecordPlay(meetingMoid, params)
		.then(() => {})
		.catch((x) => {
			Alert(x.description || t('操作失败'))
		})
}
/**停止放像 */
export const stopMeetingPlayback = () => {
	let meetingMoid = useMccStore.getState().mccInfo.meetingMoid
	fetchPostRecordStop(meetingMoid).then(() => {})
}
/**暂停放像 */
export const pauseMeetingPlayback = () => {
	let meetingMoid = useMccStore.getState().mccInfo.meetingMoid
	let state = 1
	fetchPostRecordPauseOrPlay(meetingMoid, state).then(() => {})
}
export const resumeMeetingPlayback = () => {
	let meetingMoid = useMccStore.getState().mccInfo.meetingMoid
	let state = 2
	fetchPostRecordPauseOrPlay(meetingMoid, state).then(() => {})
}
export const getPercent = (playback_duration: string, duration: string) => {
	let left = (Number(playback_duration) / Number(duration)) * 100
	let percent = Math.floor(left)
	return percent
}
